package com.jhx.mall.dao.impl;

import com.jhx.mall.dao.BaseDao;
import com.jhx.mall.dao.DtsUserDao;
import com.jhx.mall.entity.*;
import com.jhx.mall.vo.DtsFootprintVo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DtsUserDaoImpl extends BaseDao implements DtsUserDao {

    //用户基本信息
    @Override
    public List<DtsUser> selectUserInfo(int id) {
        List<DtsUser> list = new ArrayList<>();
        String sql = "select id, username, password, gender, birthday, last_login_time, " +
                "last_login_ip, user_level, nickname, mobile, avatar, " +
                "weixin_openid, status, add_time, update_time, deleted, " +
                "share_user_id from dts_user where deleted = 0 and id = ?";
        rs = super.executeQuery(sql, id);
        try {
            while (rs.next()) {
                DtsUser a = new DtsUser();
                a.setId(rs.getLong("id"));
                a.setUsername(rs.getString("username"));
                a.setPassword(rs.getString("password"));
                a.setGender(rs.getLong("gender"));
                a.setMobile(rs.getString("mobile"));
                a.setNickname(rs.getString("nickname"));
                a.setAvatar(rs.getString("avatar"));
                a.setWeixinOpenid(rs.getString("weixin_openid"));
                a.setUserLevel(rs.getLong("user_level"));
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    //商品收藏
    @Override
    public List<DtsGoods> selectMyCollect(int userId) {
        List<DtsGoods> list = new ArrayList<>();
        String sql = "select g.name,brief,retail_price,pic_url\n" +
                "from dts_collect c inner join dts_user u on c.user_id = ?\n" +
                "inner join dts_goods g on c.value_id = g.id";
        rs = super.executeQuery(sql, userId);
        try {
            while (rs.next()) {
                DtsGoods a = new DtsGoods();
                a.setName(rs.getString("name"));
                a.setBrief(rs.getString("brief"));
                a.setRetailPrice(rs.getDouble("retail_price"));
                a.setPicUrl(rs.getString("pic_url"));
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }
    //浏览足迹
    @Override
    public List<DtsFootprintVo> selectMyFootprint(int userId) {
        List<DtsFootprintVo> list = new ArrayList<>();
        String sql = "select name,brief,retail_price,pic_url,f.add_time\n" +
                "from dts_footprint f inner join dts_user u on f.user_id = u.id\n" +
                "inner join dts_goods g on f.goods_id = g.id where user_id = ?";
        rs = super.executeQuery(sql, userId);
        try {
            while (rs.next()) {
                DtsFootprintVo a = new DtsFootprintVo();
                a.setName(rs.getString("name"));
                a.setBrief(rs.getString("brief"));
                a.setRetail_price(rs.getDouble("retail_price"));
                a.setPic_url(rs.getString("pic_url"));
                a.setAdd_time(rs.getString("add_time"));
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }
    //我的拼团
    @Override
    public List<DtsGroupon> selectMyGroupon(int userId) {
        List<DtsGroupon> list = new ArrayList<>();
        String sql = "";
        rs = super.executeQuery(sql, userId);
        try {
            while (rs.next()) {
                DtsGroupon a = new DtsGroupon();
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    //意见反馈
    @Override
    public int addFeedback(DtsFeedback feedback) {
        String sql = "insert into dts_feedback(user_id,username,mobile,feed_type,content,status,has_picture,pic_urls,add_time,update_time) values(?,?,?,?,?,?,?,?,?,?)";
        return super.executeUpdate(sql, feedback.getUserId(), feedback.getUsername(), feedback.getMobile(), feedback.getFeedType(), feedback.getContent(), feedback.getStatus(), feedback.getHasPicture(), feedback.getPicUrls(), feedback.getAddTime(), feedback.getUpdateTime());
    }

    @Override
    public int addCollect(DtsCollect collect) {
        String sql = "insert into dts_collect(user_id,value_id,type,add_time,update_time) values(?,?,?,NOW(),NOW())";
        return super.executeUpdate(sql, collect.getUserId(), collect.getValueId(), collect.getType());
    }

    @Override
    public int delCollect(DtsCollect collect) {
        conn = getConnection();
        String sql = "update dts_collect set deleted = 1 where deleted = 0";
        if (collect.getId() != 0) {
            sql += " and id = " + collect.getId();
        }
        if (collect.getUserId() != 0) {
            sql += " and user_id = " + collect.getUserId();
        }
        if (collect.getValueId() != 0) {
            sql += " and value_id = " + collect.getValueId();
        }
        if (collect.getType() != -1) {
            sql += " and type = " + collect.getType();
        }
        int count = 0;
        try {
            pstmt = conn.prepareStatement(sql);
            count = pstmt.executeUpdate();
//            rs = pstmt.getGeneratedKeys();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs, pstmt, conn);
        }
        return count;
    }

}
